<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <div class="h-r-nsl">
          <ul class="nav">
            <!-- 下面这些是固定路由 -->
            <!-- 这里的页面转换有一个固定的格式 -->
            <!-- 在pages里面创建文件夹cource -->
            <!-- 在cource文件夹下创建index.vue -->
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>名师</a>
            </router-link>
            <!-- <router-link to="/my" tag="li" active-class="current">
              <a>个人中心</a>
            </router-link> -->
            <router-link
              to="/my"
              tag="li"
              active-class="current"
            >
              <a>个人中心</a>
            </router-link>

          </ul>

          <!-- / nav -->
          <ul class="h-r-login">
            <!-- 未登录显示开始 -->
            <li v-if="!loginInfo.id" id="no-login">
              <a href="/login" title="登录">
                <em class="icon18 login-icon">&nbsp;</em>
                <span class="vam ml5">登录</span>
              </a>
              |
              <a href="/register" title="注册">
                <span class="vam ml5">注册</span>
              </a>
            </li>
            <li v-if="loginInfo.id" id="is-login-one" class="mr10">
              <a id="headerMsgCountId" href="#" title="消息">
                <em class="icon18 news-icon">&nbsp;</em>
              </a>
              <q class="red-point" style="display: none">&nbsp;</q>
            </li>
            <!-- 未登录显示结束 -->

            <!-- 已登录显示开始 -->
            <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
              <a href="/my" title>
                <img
                  :src="loginInfo.avatar"
                  width="30"
                  height="30"
                  class="vam picImg"
                  alt
                />
                <span id="userName" class="vam disIb">{{
                  loginInfo.nickname
                }}</span>
              </a>
              <a
                href="javascript:void(0);"
                title="退出"
                @click="logout()"
                class="ml5"
                >退出</a
              >
            </li>
            <!-- 已登录显示结束 -->
            <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>

          <aside class="h-r-search">
            <label class="h-r-s-box">
              <input
                type="text"
                placeholder="输入你想学的课程"
                v-model="courseName"
              />
              <button type="submit" class="s-btn" @click="searchCourseByName()">
                <em class="icon18">&nbsp;</em>
              </button>
            </label>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->

    <nuxt />

    <!-- 公共底部引入被我删了 -->
  </div>
</template>
<script>
// import "~/assets/css/reset.css";
// import "~/assets/css/theme.css";
// import "~/assets/css/global.css";
// import "~/assets/css/web.css";
import "~/assets/css/reset.css";
import "~/assets/css/theme.css";
import "~/assets/css/global.css";
import "~/assets/css/web.css";
import "~/assets/css/base.css";
import "~/assets/css/activity_tab.css";
import "~/assets/css/bottom_rec.css";
import "~/assets/css/nice_select.css";
import "~/assets/css/order.css";
import "~/assets/css/swiper-3.3.1.min.css";
import cookie from "js-cookie";
import loginApi from "@/api/login";

export default {
  data() {
    return {
      token: "",
      loginInfo: {
        id: "",
        avatar: "",
        mobile: "",
        nickname: "",
        sex: "",
      },
      courseName: "",
    };
  },
  // 在页面渲染之前执行
  created() {
    var jsonStr = cookie.get("youth_token");
    console.log(jsonStr);
    if (jsonStr != "") {
      this.showInfo();
    }
  },
  methods: {
    // 创建方法，从cookie中获取用户信息
    showInfo() {
      // 从cookie获取用户信息
      var userStr = cookie.get("loginInfo");
      // console.log(userStr)
      // 把字符串转换为json对象的格式
      if (userStr != null) {
        console.log("用户信息不为空");
        this.loginInfo = JSON.parse(userStr);
        console.log(this.loginInfo);
      }
    },
    // 一点击退出按钮就执行这个方法
    logout() {
      // 清除cookie里面的数据
      cookie.set("youth_token", "", { domain: "localhost" });
      cookie.set("loginInfo", "", { domain: "localhost" });
      // 后台登出
      loginApi.userLogout();
      // 跳转到首页面
      window.location.href = "/"; 
    },
    searchCourseByName() {
      console.log(this.courseName);
      // 跳转页面
      // this.$route.push('/course/' + 1192252213659774977)
      location.href = "course?title=" + this.courseName;
    },
  },
};
</script>